Hey there.
Welcome back.
In this lecture we're going to be taking a quick look at what an enum or enumerated type is.
An enum is simply a data type that consists of a set of constant values.
These constants are used in conjunction with other APIs to define presets for specific things.
In other words, an enum is simply a container of constant values with meaningful names to make it easier
for us to code.
Imagine if we wanted to have a set of ten possible player states for a player in our game, and yet
we never defined a constant value to represent each one of those ten player states.
That would be a total mess.
However, we could have an enum to represent the state of a player when they're walking or jumping or
falling over.
However, if we didn't have that enum, we would have no idea what value to set on a player.
To give us that particular state, we first have to define that constant value to give us that state,
and then we can bundle all those values together, give them each meaningful names like jumping or falling
over, and bam, you've got yourself an enum.
I'm going to create a script real quick and server script service to demonstrate what an enum is, and
you should know to access all of the enums in the Roblox API, you just type out the keyword of enum,
and then you can index it.
And here it will show you all of the different enums that exist in the game.
And these enums are used for many different things, and they help us interact with the many services
and API functions that we have in the Roblox API.
For example, one of these enums is called a part type.
And if we index the part type enum here, it shows us a set of constants that we can go ahead and apply
to a part in our game to change its shape.
So let's go ahead and demonstrate that I'm going to create a new part.
And I'm going to position my part above the origin in the workspace.
So that'll be equal to vector three dot new.
And we'll just put it ten studs above the origin point.
We'll make sure this part is anchored.
And then I'm going to set its parent equal to the workspace.
And when we set it into the workspace you'll see it's just a regular part or a block.
But then I'm going to yield here for five seconds.
And then I'm going to update the shape property.
And here it tells us it takes an enum of part type.
And we can go ahead and change the shape of our part.
For example we could change it to the part type of ball.
And then I'm going to wait for another five seconds.
And then we can change the shape maybe to an enum part type of let's say cylinder.
We can wait another five seconds and then we can change the shape maybe to an enum part type of let's
say wedge.
Each of these represents a constant value, and these are just the names that Roblox has given to them.
So one value, they've called it ball, which will set the shape of the part equal to ball.
Another one they called it cylinder which will set the shape equal to a cylinder, and so on.
So an enum is simply just a set of constant values with meaningful names.
So if we go and run our game here.
As you can see here is our regular brick and then it should change into a sphere.
There you go.
There's our little sphere right there.
And then it should change into a cylinder.
There's our cylinder and then it should change into a wedge.
Perfect.
There we go.
Here is our wedge.
Earlier I mentioned talking about having an enum for the different states a player could be in, and
that actually exists within the Roblox API specifically for the humanoid instance.
So every player's character in the game is going to have a humanoid, and I'm just going to create one
here real quick.
I'll create a new humanoid, and these humanoids have a function inside of them where we are able to
change the state of the humanoid, and it takes an enum of humanoid state type.
So if we access the humanoid state type enum and we index it here, it shows us all the different constants
or possible values that a humanoid could be in.
For example, they could be in a dead state, they could be in a state where they're falling down or
they're jumping.
Maybe they're running or they're seated.
They could be in a state of free falling or they're swimming, stuff like that.
So there's a whole bunch of different constants here to define the different states that a humanoid
could be in.
We also have another enum when it relates to user input for example, how do we know what particular
key a player is pressing on their keyboard?
Well, thankfully we have an enum called Keycode, which stores all of the different keys that a player
could possibly press on their keyboard.
So that way when a player does press a keyboard, it'll tell us what enum Keycode it was, and then
we can go ahead and compare that key code to another key code.
And then if it matches, then we can execute some functionality based on what key a player presses on
their keyboard.
We also have another enum for storing specific fonts in our game.
So this is called font.
And here are a bunch of different fonts that we can set for stuff in our game.
We also have an enum for font sizes.
So if we access that there, boom!
We have a whole bunch of standardized font sizes where if we have different font styles, different
font weights, there is just a whole bunch of different enums in the Roblox API.
So as I stated, an enum is just a set of standardized values to enable us to communicate between the
different functions and services in our game.
If no enums existed, we would have no idea what values to set in properties or passed to functions
to get the state we want.
That's all I have for you in this lecture.
Enums are quite simple and I'll see you in the next one.